package com.xzp.blog.service.impl;

import com.xzp.blog.entity.Article;
import com.xzp.blog.entity.Tag;
import com.xzp.blog.entity.TagArticle;
import com.xzp.blog.entity.dto.form.BlogAddForm;
import com.xzp.blog.entity.dto.form.BlogModifyForm;
import com.xzp.blog.entity.dto.request.TableKeyModel;
import com.xzp.blog.entity.vo.ArticleDataGridView;
import com.xzp.blog.mapper.ArticleMapper;
import com.xzp.blog.mapper.TagArticleMapper;
import com.xzp.blog.mapper.TagMapper;
import com.xzp.blog.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements IArticleService {


    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private TagArticleMapper tagArticleMapper;

    @Override
    public Article getArticleById(Integer id) {
        return articleMapper.selectByPrimaryKey(id);  //提供的默认实现方法 根据主键查找
    }

    /**
     * 添加博客文章的实现类
     * @param form 表单
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void blogAdd(BlogAddForm form) {
        Article article = new Article("new");
        article.setTitle(form.getTitle());
        article.setMdMaterial(form.getMdMaterial());
        article.setHtmlMaterial(form.getHtmlMaterial());
        article.setIntroduction(form.getDescription());

        //处理article
        articleMapper.insert(article);
        Integer articleId = article.getId();  //可以获得ID吗

        //处理Tags
        String[] tags = form.getRawTags().split(",");

        for (String tag : tags) {
            Tag tagTemp = tagMapper.selectTagByName(tag);
            if (tagTemp != null){  //如果这个标签已经存在,则建立关联
                Integer id = tagTemp.getId();  //标签id
                TagArticle tagArticle = new TagArticle("new");
                tagArticle.setArticleId(articleId);
                tagArticle.setTagId(id);


                tagArticleMapper.insert(tagArticle);  //插入到tagArticle的映射表中
            }else {
                Tag tag1 = new Tag("new");  //构造函数中完成时间的修改
                tag1.setName(tag);


                tagMapper.insertSelective(tag1);  //插入tag1,并获得id
                Integer tagId = tag1.getId();
                TagArticle tagArticle = new TagArticle("new");
                tagArticle.setTagId(tagId);
                tagArticle.setArticleId(articleId);

                //存在Bug
                tagArticleMapper.insertSelective(tagArticle);  //插入标签文章对应关系表
            }
        }

    }

    /**
     * 修改博客文章实现类
     * @param form 表单
     */
    @Override
    public void blogModify(BlogModifyForm form) {
        Article article = new Article("old");
        article.setId(form.getId());  //这个id是传递过来的
        article.setIntroduction(form.getDescription());
        article.setHtmlMaterial(form.getHtmlMaterial());
        article.setMdMaterial(form.getMdMaterial());
        article.setTitle(form.getTitle());
        article.setGmtModified(new Date());

        //更新数据库中的信息
        articleMapper.updateByPrimaryKeySelective(article);
    }

    /**
     * 获取后台全部博客的实现类
     * @return
     */
    @Override
    public List<ArticleDataGridView> getArticleList() {
        List<Article> articleList = articleMapper.selectAll();
        List<ArticleDataGridView> articleDataGridViewList = new ArrayList<>();

        for (Article article : articleList) {
            articleDataGridViewList.add(new ArticleDataGridView(article));
        }

        return articleDataGridViewList;
    }

    /**
     * 批量删除文章的实现
     * @param model id list 数据模型
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void blogDelete(TableKeyModel model) {
        List<Integer> list = model.getIds();
        for (Integer id : list) {
            articleMapper.deleteByPrimaryKey(id);
        }

    }
}
